layui.use(['form', 'layer', 'laydate', 'table', 'upload'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table,
        laypage = layui.laypage;
    var tableIns = table.render({});
    var limitcount = 10;
    var curnum = 1;
    var method_name = 'SpotType'
    initData();

    function initData(name) {
        if (name) {
            url = public_url + 'public/?service=' + method_name + '.GetList'
        } else {
            url = public_url + 'public/?service=' + method_name + '.GetList'
        }
        $.ajax({
            url: url,
            type: "post",
            data: {page: curnum, perpage: limitcount, word: name},
            success: function (data) {
                var total_size = data.data.total;
                tableIns = table.render({
                    elem: '#linkList',
                    page: false,
                    limit: 10,
                    width: 860,
                    id: "linkListTab",
                    count: total_size,
                    cols: [[
                        {type: "checkbox", fixed: "left", width: 50},
                        {field: 'id', title: 'ID', width: 50, align: "center"},
                        {field: 'name', title: '景区分类名', width: 180, align: "center"},
                        {field: 'img', title: '景区分类图片地址', width: 175, align: "center"},
                        // {field: 'cityid', title: '城市id', width: 100, align: "center"},
                        {field: 'paixu', title: '位置次序编号', width: 120, align: "center"},

                        {title: '操作', width: 176, templet: '#orderListBar', fixed: "right", align: "center"}
                    ]],
                    data: data.data.items,
                    done: function (res, curr, count) {
                        laypage.render({
                            elem: 'test1'
                            , count: total_size
                            , curr: curnum
                            , limit: limitcount
                            , layout: ['prev', 'page', 'next', 'skip', 'count']
                            , jump: function (obj, first) {
                                if (!first) {
                                    curnum = obj.curr;
                                    limitcount = obj.limit;
                                    initData()
                                }
                            }
                        })

                    }
                });
            },
            error: function (data) {

            }
        })
    }

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click", function () {
        var search = $(".searchVal").val();
        limitcount = 10;
        curnum = 1;
        initData(search)
    });

    function addLink(edit) {
        var title = "添加景点分类";
        if (edit) {
            title = "修改景点分类";
        }
        layer.open({
            title: title,
            type: 2,
            area: ["420px", "290px"],
            content: "page/spotType/spotTypeAdd.html",
            success: function (layero, index) {
                var body = $($(".layui-layer-iframe", parent.document).find("iframe")[0].contentWindow.document.body);
                if (edit) {

                    body.find(".id").val(edit.id);
                    body.find(".name").val(edit.name);
                    body.find(".img").val(edit.img);
                    body.find(".cityid").val(edit.cityid);
                    body.find(".paixu").val(edit.paixu);

                    form.render();
                }
            }
        })
    }

    $(".add_btn").click(function () {
        addLink();
    });

    //批量删除
    $(".delAll_btn").click(function () {
        var checkStatus = table.checkStatus('linkListTab'),
            data = checkStatus.data;
        if (data.length > 0) {
            layer.confirm('确定删除选中的景点分类吗？', {icon: 3, title: '提示信息'}, function (index) {
                var size = data.length;
                for (var i in data) {
                    $.ajax({
                        url: public_url + 'public/?service=App.' + method_name + '.Delete',
                        type: "post",
                        data: {id: data[i].id},
                        success: function (data) {
                            if (i == size - 1) {
                                tableIns.reload();
                                initData();
                                layer.close(index);
                                layer.msg(data.data.msg)
                            }
                        }
                    })
                }

            })
        } else {
            layer.msg("请选择需要删除的景点分类");
        }
    });

    //每列的编辑和删除
    table.on('tool(linkList)', function (obj) {
        var layEvent = obj.event,
            data = obj.data;
        if (layEvent === 'edit') { //编辑
            addLink(data);
        } else if (layEvent === 'del') { //删除
            layer.confirm('确定删除此景点分类吗？', {icon: 3, title: '提示信息'}, function (index) {
                $.ajax({
                    url: public_url + 'public/?service=App.' + method_name + '.Delete',
                    type: "post",
                    data: {id: data.id},
                    success: function (data) {
                        tableIns.reload();
                        initData();
                        layer.close(index);
                        layer.msg(data.data.msg);
                    }
                })
            });
        }
    });

    form.on("submit(addLink)", function (data) {
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.8});
        if ($(".id").val()) {
            method = public_url + 'public/?service=App.' + method_name + '.Update';
        } else {
            method = public_url + 'public/?service=App.' + method_name + '.Insert';
        }
        $.ajax({
            url: method,
            type: "post",
            data: {id: $(".id").val(),name: $(".name").val(), img: $(".img").val(), cityid: $(".cityid").val(), paixu: $(".paixu").val()},
            success: function (data) {
                top.layer.close(index);
                layer.closeAll("iframe");
                top.layer.msg(data.data.msg);
                //刷新父页面
                $(".layui-tab-item.layui-show", parent.document).find("iframe")[0].contentWindow.location.reload();
                tableIns.reload();
                initData();
            }
        });
        return false;
    })
});
